package model

import (
	"gin-project/utils"
	"gin-project/utils/errmsg"
	"gorm.io/gorm"
	"mime/multipart"
	"strconv"
)

type Singer struct {
	gorm.Model
	Name         string `json:"name"`
	Sex          int    `json:"sex"`
	Pic          string `json:"pic"`
	Birth        string `json:"birth"`
	Location     string `json:"location"`
	Introduction string `json:"introduction"`
	Songs        []Song
}

func (Singer) TableName() string {
	return "singer"
}

// 增加歌手
func InsertSinger(singer *Singer) int {
	err := db.Create(&singer).Error
	if err != nil {
		return errmsg.SINGER_ADD_FAILED
	}
	return errmsg.SUCCESS
}

// 更新歌手信息
func UpdateSinger(m map[string]interface{}, id string) int {
	err := db.Model(&Singer{}).Where("id = ?", id).Updates(m).Error
	if err != nil {
		return errmsg.SINGER_UPDATE_FAILED
	}
	return errmsg.SUCCESS
}

// 根据id删除歌手
func DeleteSinger(id string) int {
	convId, _ := strconv.Atoi(id)
	err := db.Where("id = ?", convId).Delete(&Singer{}).Error
	if err != nil {
		return errmsg.SINGER_DELETE_FAILED
	}
	return errmsg.SUCCESS
}

// 根据id查询歌手
func SelectSingerById(id int) int {
	return 0
}

// 查询所有歌手
func SelectSingers() ([]Singer, int) {
	var singers []Singer
	err := db.Find(&singers).Error
	if err != nil {
		return nil, errmsg.SINGER_SELECT_FAILED
	}
	return singers, errmsg.SUCCESS
}

// 根据名字模糊查询歌手
func SelectSingersByName(name string) int {
	return 0
}

// 根据性别查询歌手
func SelectSingersBySex(sex int) int {
	return 0
}

// 更新歌手图片
func UpdateSingerPic(file multipart.File, header *multipart.FileHeader) (string, int) {
	url, code := utils.UploadFile(file, header.Size)
	return url, code
}
